package org.phantom.securityframework.api.factory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Springbean工厂
 * @author david
 *
 */
public class SpringManagerFactory implements ManagerFactory{

	protected String configPath = "classpath*:spring/*.xml";
	protected ApplicationContext ctx = null;
	public void setConfigPath(String configPath){
		this.configPath = configPath;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Object getService(Class parentClass) {
		if(ctx == null)
			ctx = this.getApplicationContext();
		
		return ctx.getBean(parentClass);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Object> getServices(Class parentClass) {
		if(ctx ==null)
			ctx = this.getApplicationContext();
		Map<String,?> map = ctx.getBeansOfType(parentClass);
		List<Object> temp = new ArrayList<Object>();
		for (String s : map.keySet()) {
			temp.add(map.get(s));
		}
		return temp;
	}

	
	protected ApplicationContext getApplicationContext(){
		if(configPath==null || "".equals(configPath)){
			configPath="classpath*:spring/*.xml";
		}
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
		return ctx;
	}

	

}
